<template>
  <el-dialog
    title="绑定马甲"
    :visible.sync="bindVestDialog.isShow"
    width="900px"
    @close="closeBindDialog"
  >
    <div>
      <!-- 顶部搜索部分 -->
      <div id="topSearch">
        <div class="search-box">
          <el-form
            ref="ruleForm"
            :inline="true"
            :model="formInline"
            :rules="rules"
            class="demo-form-inline"
          >
            <el-form-item label="选择运营人员:" prop="dapengUserId">
              <el-select
                v-model="formInline.dapengUserId"
                filterable
                placeholder=""
              >
                <el-option
                  v-for="(item, index) in operaPeopleList"
                  :key="'item' + index"
                  :label="item.nickname"
                  :value="item.dapengUserId"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- 输入搜索部分 -->
            <el-form-item label="查 询:" prop="content">
              <el-input
                clearable
                v-model="formInline.content"
                placeholder="请输入马甲昵称/手机号/大鹏号查询"
                size="small"
                class="numberInput"
              ></el-input>
            </el-form-item>
            <el-form-item class="s-btn">
              <el-button
                type="success"
                icon="el-icon-search"
                @click="toSearch('ruleForm')"
              >
                查询
              </el-button>
              <el-button type="success" plain @click="reset">
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="table-conBtn">
        <freshCom @searchList="searchList"></freshCom>
      </div>
      <!-- 表格部分 -->
      <div id="table">
        <el-table
          v-loading="loading"
          :data="tableData"
          style="width: 100%"
          height="400"
          @selection-change="selectChange"
        >
          <el-table-column
            type="selection"
            width="55"
            align="center"
          ></el-table-column>
          <el-table-column
            type="index"
            width="50"
            align="center"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="robotAvatar"
            label="马甲头像"
            width="80"
            align="center"
          >
            <template slot-scope="scope">
              <el-avatar :src="scope.row.robotAvatar"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column
            prop="robotNickname"
            label="马甲昵称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="dpAccount"
            label="大鹏号"
            min-width="50"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="fans"
            label="粉丝数"
            min-width="50"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="robotMobile"
            label="手机号"
            width="120"
            align="center"
          ></el-table-column>
        </el-table>
        <!--分页-全局组件-->
        <pageCom
          :searchParams="searchParams"
          :total="total"
          @searchList="searchList"
        ></pageCom>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="success" plain @click="closeBindDialog">取 消</el-button>
      <el-button type="success" @click="bindVest">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  addSubForm,
  getBindVest,
  bindSubmit
} from '@/api/vestManagement/vestManage' // 接口
import { getDpNum } from '@/api/system/user'
export default {
  props: ['bindVestDialog'],
  data() {
    return {
      loading: true,
      total: 0,
      tableData: [], // table内容
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      // 表单绑定数据
      formInline: {
        dapengUserId: '',
        content: ''
      },
      // 运营人员列表
      operaPeopleList: [],
      rules: {
        dapengUserId: {},
        content: {}
      },
      selectData: [] // 多选框选中的数据
    }
  },
  mounted() {
    this.getStickList()
    this.searchList()
  },
  methods: {
    /* 获取选择运营人员 */
    getStickList() {
      getDpNum({ type: 'OPERATORS' }, (response) => {
        this.operaPeopleList = response
        this.formInline.dapengUserId = this.operaPeopleList[0].dapengUserId
      })
    },
    /* 获取列表 */
    searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      obj.unbound = 0
      delete obj.dapengUserId
      getBindVest(
        obj,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },

    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.formInline.dapengUserId = this.operaPeopleList[0].dapengUserId
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    // 多选框按钮改变事件
    selectChange(value) {
      this.selectData = value
    },
    // 点击确定
    bindVest() {
      // let _this = this
      if (this.selectData.length === 0) {
        this.$alert('请至少选择一条记录', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        this.$confirm('确认绑定马甲吗?', '绑定提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let robotIds = []
          this.selectData.forEach((item) => {
            robotIds.push(item.robotId)
          })
          this.handleItem(robotIds)
        })
      }
    },
    handleItem(robotIds) {
      let obj = { robotIds }
      obj.userId = this.formInline.dapengUserId
      let params = this.changeToForm(obj)
      bindSubmit(
        obj,
        (response) => {
          this.loading = false
          this.$emit('refresh')
          this.searchList()
        },
        (response) => {
          this.loading = false
          this.$message({
            message: '失败' + response.message,
            type: 'warning'
          })
        }
      )
    },
    // 关闭对话框 清除表单数据
    closeBindDialog() {
      this.loading = false
      this.formInline.content = ''
      this.selectData = []
      this.bindVestDialog.isShow = false
    }
  }
}
</script>
<style lang="less" scoped>
.el-input {
  width: 220px;
}
</style>
